package com.xywy.bloodandroid.util;

import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;

public class HttpUtil {
	static DefaultHttpClient client ;
	//发送get请求
	 public static void httpclientDoGet(final String url, final ResponseCallBack callBack){
		 new Thread(){
			 @Override
			public void run() {
				 try {
					 if(client == null ){
						 client = new DefaultHttpClient();
					 } 
					 HttpGet get = new HttpGet(url);
					 HttpResponse response = client.execute(get);
					 if(response.getStatusLine().getStatusCode() == 200){
						 //网络没问题
						 HttpEntity entity = response.getEntity();  
				            InputStream is = entity.getContent();  
				            byte[] bytes = new byte[256];  
				            StringBuffer sb = new StringBuffer();  
				            while (is.read(bytes) > 0) {  
				                sb.append(new String(bytes));  
				                bytes = new byte[256];  
				            }  
				            System.out.println(sb.toString());
				            callBack.success(sb.toString());
					 }
				} catch (Exception e) {
					callBack.error(e);
					e.printStackTrace();
				}
			}
		 }.start();
	}
	 
	//发送get请求
		 public static void httpclientDoPost(final String url, final ResponseCallBack callBack,final HashMap<String,String> params){
			 new Thread(){
				 @Override
				public void run() {
					 try {
						 if(client == null ){
							 client = new DefaultHttpClient();
						 } 
						 HttpPost post = new HttpPost(url);
						 List<BasicNameValuePair> pairs = new ArrayList<BasicNameValuePair>();  
						Set<Map.Entry<String, String>> sets = params.entrySet();
						Iterator<Map.Entry<String, String>> iterator = sets.iterator();
						while(iterator.hasNext()){
							Map.Entry<String, String> entry = iterator.next();
							pairs.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
						}
						 post.setEntity(new UrlEncodedFormEntity(pairs, HTTP.UTF_8));
						 HttpResponse response = client.execute(post);
						 if(response.getStatusLine().getStatusCode() == 200){
							 //网络没问题
							 HttpEntity entity = response.getEntity();  
					            InputStream is = entity.getContent();  
					            byte[] bytes = new byte[256];  
					            StringBuffer sb = new StringBuffer();  
					            while (is.read(bytes) > 0) {  
					                sb.append(new String(bytes));  
					                bytes = new byte[256];  
					            }  
					            System.out.println(sb.toString());
					            callBack.success(sb.toString());
						 }
					} catch (Exception e) {
						callBack.error(e);
						e.printStackTrace();
					}
				}
			 }.start();
		}
	 
	 public interface ResponseCallBack{
		 void error(Exception e);
		 void success(String result);
	 }
}
